<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="../assets/js/vue.2.0.1.js"></script>
    <title>V-for 案例</title>
</head>

<body>
    <h1>v-for指令用法</h1>
    <hr>
    <div id="app">
        <div>简单数组-排序之前</div>
        <ul>
            <li v-for="item in items1">
                {{item}}
            </li>
        </ul>
        <div>简单数组-排序之后</div>
        <ul>
            <li v-for="item in sortItems">
                {{item}}
            </li>
        </ul>
        <div>对象数组-循环输出</div>
        <ul>
            <li v-for="item in students1">
                {{item.name}}-{{item.age}}岁
            </li>
        </ul>
        <div>对象数组-循环输出（带索引序号）</div>
        <ul>
            <li v-for="(item,index) in students1">
                {{index+1}}.{{item.name}}-{{item.age}}岁
            </li>
        </ul>
        <div>对象数组-按照指定key排序</div>
        <ul>
            <li v-for="(item,index) in sortStudents">
                {{index+1}}.{{item.name}}-{{item.age}}岁
            </li>
        </ul>
        <div>对象数组-去重</div>
        <ul>
            <li v-for="(item,index) in uniqueStudents">
                {{index+1}}.{{item.name}}-{{item.age}}岁
            </li>
        </ul>
    </div>

    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data: {
                items1: [20, 23, 18, 65, 32, 19, 54, 56, 41],
                items2: [20, 23, 18, 65, 32, 19, 54, 56, 41],
                students1: [{
                        name: 'jspang',
                        age: 32
                    },
                    {
                        name: 'Panda',
                        age: 30
                    },
                    {
                        name: 'PanPaN',
                        age: 21
                    },
                    {
                        name: 'King',
                        age: 45
                    }
                ],
                students2: [{
                        name: 'jspang',
                        age: 32
                    },
                    {
                        name: 'Panda',
                        age: 30
                    },
                    {
                        name: 'PanPaN',
                        age: 21
                    },
                    {
                        name: 'King',
                        age: 45
                    }
                ],
                students3: [{
                        name: 'jspang',
                        age: 32
                    },
                    {
                        name: 'Panda',
                        age: 30
                    },
                    {
                        name: 'PanPaN',
                        age: 21
                    },
                    {
                        name: 'King',
                        age: 45
                    }, {
                        name: 'Panda',
                        age: 30
                    }, {
                        name: 'jspang',
                        age: 32
                    }
                ]
            },
            computed: {
                sortItems: function () {
                    return this.items2.sort(sortNumber);
                },
                sortStudents: function () {
                    return sortBykey(this.students2, 'age');
                },
                uniqueStudents: function () {
                    return uniqueArray(this.students3, 'age');
                }
            }
        })
        // 普通数组排序
        function sortNumber(a, b) {
            return a - b
        }
        // 对象数组排序(按照指定key排序)
        function sortBykey(array, key) {
            return array.sort(function (a, b) {
                var x = a[key];
                var y = b[key];
                return ((x < y) ? -1 : ((x > y) ? 1 : 0));
            })
        }
        // 通用JSON数组去重
        function uniqueArray(array, key) {
            var result = [array[0]];
            for (var i = 1; i < array.length; i++) {
                var item = array[i];
                var repeat = false;
                for (var j = 0; j < result.length; j++) {
                    if (item[key] == result[j][key]) {
                        repeat = true;
                        break;
                    }
                }
                if (!repeat) {
                    result.push(item);
                }
            }
            return result;
        }
    </script>
</body>

</html>